﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BK_CLIENT
{
    public partial class PROGRESS_RECEIVED_FILE : Form
    {
        string receiveText;
        SaveFileDialog saveFile;
        public PROGRESS_RECEIVED_FILE(SaveFileDialog fileSave, string fileRecv)
        {
            InitializeComponent();
            receiveText = fileRecv;
            saveFile = fileSave;
            this.progressBarReceivedFile.Maximum = receiveText.Length;
            this.progressBarReceivedFile.Value = 0;
            this.timerReceivedFile.Interval = 1;
            this.timerReceivedFile.Enabled = true;
        }

        private void timerReceivedFile_Tick(object sender, EventArgs e)
        {
            if (this.progressBarReceivedFile.Value < this.progressBarReceivedFile.Maximum)
            {
                int len;
                string oneRecv;
                if (this.progressBarReceivedFile.Value + 1000 < this.progressBarReceivedFile.Maximum)
                {
                    len = 1000;
                }
                else
                {
                    len = this.progressBarReceivedFile.Maximum - this.progressBarReceivedFile.Value;
                }
                this.progressBarReceivedFile.Value += len;
                oneRecv = receiveText.Substring(0,len);
                receiveText = receiveText.Remove(0,len);
                File.AppendAllText(saveFile.FileName, oneRecv);
            }
            else 
            {
                int i = 0;
                while(i<100)
                {
                    i++;
                }
                this.timerReceivedFile.Enabled = false;
                MessageBox.Show("Nhận file thành công");
                this.Close();
            } 
        }

        private void buttonCancelReceivedFile_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn dừng nhận file ", "Received File", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                File.Delete(saveFile.FileName);
                this.Close();
            }
        }
    }
}
